#include "Encoder.h"


Encoder::Encoder()
{
}


Encoder::~Encoder()
{
}


void Encoder::Encode(char* szSrcFile,char* szZipFile)
{
	FileReader cFileReader(szSrcFile,RD_SRC);
	FileWriter cFileWriter(szZipFile,WT_ZIP);

	Tree cTree(&cFileReader);
	Node** aLeaves=cTree.GetLeaves();

	int iSymbol;
	int iSymbolsCount=cFileReader.SymbolsCount();

	for (int i=0; i<iSymbolsCount; i++)
	{
		cFileReader.GetSymbol(&iSymbol);
		EncodeSymbol(aLeaves[iSymbol],&cFileWriter);
	}
}


void Encoder::EncodeSymbol(Node* pNode,FileWriter* pFileWriter)
{
	Node* pParent=pNode->Parent();
	if (pParent!=NO_NODE)
	{
		int iBit=(pNode==pParent->Child0()? 0:1);
		EncodeSymbol(pParent,pFileWriter);
		pFileWriter->AddBit(iBit);
	}
}
